package com.laicunba.ext.web.controller.web;

import javax.servlet.http.HttpServletRequest;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.service.RegistryService;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/webapi/ext")
public class BidCountAPIController extends BaseController {
	@Autowired
	private RegistryService registryService;

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "bid_count.htm", method=RequestMethod.GET)
	public String bidCount(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		
		String callback = request.getParameter("callback");
		if(StringUtil.isNotEmpty(callback)){	
			RegistryDO registryDO = registryService.get(RegistryDO.class, "sys.total.bid");
			if(registryDO != null){
				json.put("code", ResponseCode.SUCCESS);
				json.put("data", net.sf.json.JSONObject.fromObject(registryDO.getVal()));
			} else {
				json.put("code", ResponseCode.FAIL);
				json.put("data", null);
			}
		} else {
			json.put("code", ResponseCode.PARAM_ERROR);
			json.put("data", null);
		}
		
		return callback + "(" + toJsonObject(json) + ")";
	}
}
